<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Cellphonezone
 * @package     Cellphonezone_Repay
 * @copyright   Copyright (c) 2010 Cellphonezone
 * @author $Id$
 */
 
class Cellphonezone_Repay_Model_Sales_Creditmemo_Total_Payment extends Mage_Sales_Model_Order_Creditmemo_Total_Abstract
{
    public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
    {
        $order = $creditmemo->getOrder();
        $allowedAmount          = $order->getPaymentAmount()-$order->getPaymentRefunded();
        $baseAllowedAmount      = $order->getBasePaymentAmount()-$order->getBasePaymentRefunded();

        $payment               = $order->getPaymentAmount();
        $basePayment           = $order->getBasePaymentAmount();
        //$paymentInclTax        = $order->getPaymentInclTax();
        //$basePaymentInclTax    = $order->getBasePaymentInclTax();

        //$isPaymentInclTax = Mage::getSingleton('tax/config')->displaySalesPaymentInclTax($order->getStoreId());

        /**
         * Check if Payment amount was specified (from invoice or another source).
         * Using has magic method to allow setting 0 as Payment amount.
         */
        if ($creditmemo->hasBasePaymentAmount()) {
            $basePaymentAmount = Mage::app()->getStore()->roundPrice($creditmemo->getBasePaymentAmount());
            /*if ($isPaymentInclTax && $basePaymentInclTax != 0) {
                $part = $basePaymentAmount/$basePaymentInclTax;
                $paymentInclTax    = Mage::app()->getStore()->roundPrice($paymentInclTax*$part);
                $basePaymentInclTax= $basePaymentAmount;
                $basePaymentAmount = Mage::app()->getStore()->roundPrice($basePayment*$part);
            }*/
            /*
             * Rounded allowed Payment refund amount is the highest acceptable Payment refund amount.
             * Payment refund amount shouldn't cause errors, if it doesn't exceed that limit.
             * Note: ($x < $y + 0.0001) means ($x <= $y) for floats
             */
            if (abs($basePaymentAmount) < abs(Mage::app()->getStore()->roundPrice($baseAllowedAmount)) + 0.0001) {
                /*
                 * Payment refund amount should be equated to allowed refund amount,
                 * if it exceeds that limit.
                 * Note: ($x > $y - 0.0001) means ($x >= $y) for floats
                 */
                if (abs($basePaymentAmount) > abs($baseAllowedAmount) - 0.0001) {
                    $payment     = $allowedAmount;
                    $basePayment = $baseAllowedAmount;
                } else {
                    if ($basePayment != 0) {
                        $payment = $payment * $basePaymentAmount / $basePayment;
                    }
                    $payment     = Mage::app()->getStore()->roundPrice($payment);
                    $basePayment = $basePaymentAmount;
                }
            } else {
                $baseAllowedAmount = $order->getBaseCurrency()->format($baseAllowedAmount,null,false);
                Mage::throwException(
                    Mage::helper('sales')->__('Maximum payment amount allowed to refund is: %s', $baseAllowedAmount)
                );
            }
        } else {
            if ($basePayment != 0) {
                //$allowedTaxAmount = $order->getPaymentTaxAmount() - $order->getPaymentTaxRefunded();
                //$baseAllowedTaxAmount = $order->getBasePaymentTaxAmount() - $order->getBasePaymentTaxRefunded();

                //$paymentInclTax = Mage::app()->getStore()->roundPrice($allowedAmount + $allowedTaxAmount);
                //$basePaymentInclTax = Mage::app()->getStore()->roundPrice($baseAllowedAmount + $baseAllowedTaxAmount);
            }
            $payment           = $allowedAmount;
            $basePayment       = $baseAllowedAmount;
        }

        $creditmemo->setPaymentAmount($payment);
        $creditmemo->setBasePaymentAmount($basePayment);
        //$creditmemo->setPaymentInclTax($paymentInclTax);
        //$creditmemo->setBasePaymentInclTax($basePaymentInclTax);
//var_dump($creditmemo->getGrandTotal());exit;
        $creditmemo->setGrandTotal($creditmemo->getGrandTotal()+$payment);
        $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal()+$basePayment);
        return $this;
    }
}
